package cn.kgc.springmvc.controller;

import cn.kgc.springmvc.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.lang.model.element.VariableElement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * @author: mosin
 * @version: v1.0  2022/4/29
 *
 *控制层如何将业务层处理的结果返回到前端页面
 *
 */
@Controller
@RequestMapping("rv")
public class ResponseValueController {


    @RequestMapping("r1")
    public ModelAndView test01(int id){
         //1.接收参数
        System.out.println("id = " + id);
        //调用业务层查询学生信息
        Student student = Student.builder().id(id).age(23).birth(new Date()).name("jack").score(88.0).build();
        //创建ModelAndView 类型的对象  实现视图和响应数据的封装
        ModelAndView mv = new ModelAndView();
        //设置视图名
        mv.setViewName("index");
        mv.addObject("student", student);
        return mv ;   // mv ->dipaservlet - > 1.调用视图解析器 2. model设置到作用域 request -> view  转发
    }


    //视图和数据分别绑定
    @RequestMapping("r2")
    public String  test02(int id, Model model){
        //1.接收参数
        System.out.println("id = " + id);
        //调用业务层查询学生信息
        Student student = Student.builder().id(id).age(23).birth(new Date()).name("jack").score(88.0).build();
        model.addAttribute("student", student);
        return "index";
    }

    //重定向 实现页面的跳转
    @RequestMapping("r3")
    public String  test03(int id, HttpSession session){
        //1.接收参数
        System.out.println("id = " + id);
        //调用业务层查询学生信息
        Student student = Student.builder().id(id).age(23).birth(new Date()).name("jack").score(88.0).build();
        //将数据放入 session作用域
        session.setAttribute("student", student);
        return "redirect:/static/index.jsp";
    }


    //重定向 实现页面的跳转   注入 HttpServletRequest request 对象
    @RequestMapping("r4")
    public String  test04(int id, HttpServletRequest request){
        //1.接收参数
        System.out.println("id = " + id);
        //调用业务层查询学生信息
        Student student = Student.builder().id(id).age(23).birth(new Date()).name("jack").score(88.0).build();
        //将数据放入 session作用域
        HttpSession session = request.getSession();
        session.setAttribute("student", student);
        return "redirect:/static/index.jsp";
    }

    @RequestMapping("r5")
    public void  test05(int id, HttpServletRequest request,HttpServletResponse resp){
        System.out.println("r5请求被接收");
        //1.接收参数
        System.out.println("id = " + id);
        //调用业务层查询学生信息
        Student student = Student.builder().id(id).age(23).birth(new Date()).name("jack").score(88.0).build();
        //将数据放入 session作用域
        HttpSession session = request.getSession();
        session.setAttribute("student", student);
        //HttpServletResponse resp 重定向
        try {
            resp.sendRedirect(request.getContextPath()+"/static/index.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //使用转发  实现页面跳转
    @RequestMapping("r6")
    public void  test06(int id, HttpServletRequest request,HttpServletResponse resp){
        System.out.println("r6请求被接收");
        //1.接收参数
        System.out.println("id = " + id);
        //调用业务层查询学生信息
        Student student = Student.builder().id(id).age(23).birth(new Date()).name("jack").score(88.0).build();
        //将返回的数据设置在request作用域中
        request.setAttribute("student", student);
        try {
            request.getRequestDispatcher("/static/index.jsp").forward(request,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
